﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Threading;

namespace OEMS
{
    /// <summary>
    /// ImportForm
    /// </summary>
    /// <author>Robin goris</author>
    public partial class ImportForm : Form
    {

        private int saveBarIndex = 0;

        public ImportForm()
        {
            InitializeComponent();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (onderdelenComboBox.Text.Equals("Motor"))
            {
                if (rendementListBox.CheckedItems.Count < 1)
                {
                    MessageBox.Show("Selecteer minstens 1 sheet aub!");
                    return;

                }
                else if ((toerentalTxt.Text.Equals("") || Convert.ToInt32(toerentalTxt.Text) < 0 || Convert.ToInt32(toerentalTxt.Text) < 1000))
                {
                    MessageBox.Show("Geef een geldig nominaal toerental in aub!");
                    return;
                }
                else if (toerentalCBox.SelectedItem == null)
                {
                    MessageBox.Show("Selecteer een toerental sheet aub!");
                    return;

                }
                else if (koppelCBox.SelectedItem == null)
                {
                    MessageBox.Show("Selecteer een koppel sheet aub!");
                    return;
                }
            }
            else
            {
                return;

            }
            Import.koppelSheet = koppelCBox.SelectedItem.ToString();
            Import.toerentalSheet = toerentalCBox.SelectedItem.ToString();
            Import.rendementSheet = new string[rendementListBox.CheckedItems.Count];
            Import.nomToerental = Convert.ToInt32(toerentalTxt.Text);

            saveBar.Maximum = (Import.rendementSheet.Length * 2);
            saveBar.Visible = true;
            koppelCBox.Enabled = false;
            toerentalCBox.Enabled = false;
            toerentalTxt.Enabled = false;
            rendementListBox.Enabled = false;
            saveBtn.Enabled = false;
            cancelBtn.Enabled = false;

            backgroundWorker1.RunWorkerAsync();

        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           
            for (int i = 0; i < rendementListBox.CheckedItems.Count; i++)
            {
                Import.rendementSheet[i] = rendementListBox.CheckedItems[i].ToString();
            }

            //moment sheet values opslaan in een array
            Import.GetWorksheet(Import.koppelSheet);
            Import.saveSheet(Import.excelDataSet, Import.koppelSheet);
            

            //toerental sheet values opslaan in array
            Import.GetWorksheet(Import.toerentalSheet);
            Import.saveSheet(Import.excelDataSet, Import.toerentalSheet);
        //    backgroundWorker1.ReportProgress(saveBarIndex += 1);

            //rendement sheets opslaan in database
            for (int i = 0; i < Import.rendementSheet.Length; i++)
            {
                Import.sheetNaamIndex = Import.rendementSheet[i].IndexOf("kW");
                Import.klasse = Import.rendementSheet[i].Substring(Import.sheetNaamIndex + 3);
                Import.nomVerm = Convert.ToDecimal(Import.rendementSheet[i].Substring(0, Import.sheetNaamIndex));

                //controleren of de motor niet al in de database zit..
                if (Import.CheckMotorInDatabase(Import.klasse, Import.nomVerm, Import.nomToerental))
                {
                    backgroundWorker1.ReportProgress(saveBarIndex += 2);
                    //  MessageBox.Show("Dubbele motor");
                }
                else
                {
                    Import.GetWorksheet(Import.rendementSheet[i]);
                    Import.saveMotor(Import.excelDataSet, Import.rendementSheet[i]);
                    backgroundWorker1.ReportProgress(saveBarIndex += 1);
                    Import.saveSheet(Import.excelDataSet, Import.rendementSheet[i]);
                    backgroundWorker1.ReportProgress(saveBarIndex += 1);
                    
                }
            }
            Thread.Sleep(1000);
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {

            saveBar.Value = e.ProgressPercentage;
            this.Text = "Saving: " + (((saveBarIndex * 10)/saveBar.Maximum)*10).ToString() + " %";
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void onderdelenComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

            openFileDialog1.FileName = " ";
            openFileDialog1.Filter = "Excel 2007 files (*.xlsx)|*.xlsx|Excel 97-2003 files (*.xls)|*.xls";
            if (openFileDialog1.ShowDialog(this) == DialogResult.Cancel)
            {
                clearComboBoxes();
                return;
            }

            clearComboBoxes();
            Import.CheckExtentie(openFileDialog1.FileName);

            rendementListBox.Items.AddRange(Import.workSheetNames);
            koppelCBox.Items.AddRange(Import.workSheetNames);
            toerentalCBox.Items.AddRange(Import.workSheetNames);

        }
        
        private void clearComboBoxes()
        {
            toerentalCBox.Items.Clear();
            koppelCBox.Items.Clear();
            rendementListBox.Items.Clear();
        }

    }
}
